$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'mercharReport/orderinfo/list',
        datatype: "json",
        colModel: [
            {label: '订单号', name: 'orderId', index: 'order_id', width: 110, key: true},
            {label: '门店', name: 'suppliers', index: 'suppliers', width: 80},
            {label: '客户名称', name: 'memberId', index: 'memberId', width: 80},
            {label: '联系电话', name: 'shipTel', index: 'shipTel', width: 80},
            {label: '开单日期', name: 'createtime', index: 'createtime', width: 80},
            {label: '收款方式', name: 'payment', index: 'payment', width: 80},
            {label: '应收金额', name: 'payableAmount', index: 'payableAmount', width: 80},
            {label: '实收金额', name: 'payed', index: 'payed', width: 80},
            {label: '调价金额', name: 'discount', index: 'discount', width: 80},
            {label: '优惠金额', name: 'pmtGoods', width: 80},
            {label: '订单状态', name: 'productionStatus', index: 'productionStatus', width: 80},
            {label: '支付时间', name: 'paymentTime', index: 'paymentTime', width: 110},
            {label: '项目名称', name: 'projectName', index: 'project_name', width: 100},
            {label: '备注', name: 'memo', index: 'memo', width: 100},
            {label: '制作要求', name: 'productionRequest', width: 100},
            {label: '规格属性', name: 'addon', width: 100},
            {label: '业务员', name: 'merchantsSalesMan', width: 100},
            {label: '是否挂账', name: 'onCredit', width: 100},
        ],
        viewrecords: true,
        height: window.innerHeight-125,
        rowNum: 25,
        rowList: [25, 50, 80],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            //获取枚举参数
            vm.productionStatusList = $("#jqGrid").jqGrid('getGridParam', 'userData').productionStatusList;
            vm.paymentList = $("#jqGrid").jqGrid('getGridParam', 'userData').paymentList;
            vm.totalMoney = $("#jqGrid").jqGrid('getGridParam', 'userData').totalMoney;
            vm.payedMoney = $("#jqGrid").jqGrid('getGridParam', 'userData').payedMoney;
            vm.totalCount = $("#jqGrid").jqGrid('getGridParam', 'userData').totalCount;
            vm.PayStatusEnum = $("#jqGrid").jqGrid('getGridParam', 'userData').PayStatusEnum;
            vm.usernames = $("#jqGrid").jqGrid('getGridParam', 'userData').usernames;
            vm.alipayTatal = $("#jqGrid").jqGrid('getGridParam', 'userData').alipayTatal;
            vm.weixinTatal = $("#jqGrid").jqGrid('getGridParam', 'userData').weixinTatal;
            vm.cashTatal = $("#jqGrid").jqGrid('getGridParam', 'userData').cashTatal;
            vm.unTatal = $("#jqGrid").jqGrid('getGridParam', 'userData').unTatal;
            vm.moneyTatal = $("#jqGrid").jqGrid('getGridParam', 'userData').moneyTatal;
            vm.sweepcodeTatal = $("#jqGrid").jqGrid('getGridParam', 'userData').sweepcodeTatal;
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        orderInfo: {},
        q: {
            orderId: '',
            name: '',
            payment: '',
            productionStatus: '',
            beginCreatetime: '',
            endCreatetime: '',
            payStatus:'',
            merchantsSalesMan:'',
            onCredit:''
        },
        paymentList: [],
        productionStatusList: [],
        PayStatusEnum:[],
        usernames:[],
        totalMoney:'',
        payedMoney:'',
        totalCount:'',
        beginPaytime:'',
        endPaytime:'',
        alipayTatal:'',
        weixinTatal:'',
        cashTatal:'',
        unTatal:'',
        moneyTatal:'',
        sweepcodeTatal:''
    },
    methods: {
        reset() {
            vm.q.orderId = ''
            vm.q.name = ''
            vm.q.payment = ''
            vm.q.productionStatus = ''
            vm.q.beginCreatetime = ''
            vm.q.endCreatetime = ''
            vm.q.beginPaytime = ''
            vm.q.endPaytime = ''
            vm.q.payStatus=''
            vm.q.merchantsSalesMan=''
            vm.q.onCredit=''
            vm.reload();
        },
        checkbeginPaytime: function () {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd HH:mm:ss', isShowClear: false, onpicking: function (dp) {
                    vm.q.beginPaytime = dp.cal.getNewDateStr();
                }
            })
        },
        checkendPaytime: function () {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd HH:mm:ss', isShowClear: false, onpicking: function (dp) {
                    vm.q.endPaytime = dp.cal.getNewDateStr();
                }
            })
        },
        checkBeginDate: function () {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd HH:mm:ss', isShowClear: false, onpicking: function (dp) {
                    vm.q.beginCreatetime = dp.cal.getNewDateStr();
                }
            })
        },
        checkEndDate: function () {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd HH:mm:ss', isShowClear: false, onpicking: function (dp) {
                    vm.q.endCreatetime = dp.cal.getNewDateStr();
                }
            })
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.orderInfo = {};
        },
        update: function (event) {
            var orderId = getSelectedRow();
            if (orderId == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(orderId)
        },
        saveOrUpdate: function (event) {
            var url = vm.orderInfo.orderId == null ? "mercharReport/orderinfo/save" : "mercharReport/orderinfo/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.orderInfo),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var orderIds = getSelectedRows();
            if (orderIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "mercharReport/orderinfo/delete",
                    contentType: "application/json",
                    data: JSON.stringify(orderIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (orderId) {
            $.get(baseURL + "mercharReport/orderinfo/info/" + orderId, function (r) {
                vm.orderInfo = r.orderInfo;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {//postData此数组内容直接赋值到url上，参数类型：{name1:value1…}
                    'orderId': vm.q.orderId.trim(),
                    'name': vm.q.name.trim(),
                    'payment': vm.q.payment,
                    'productionStatus': vm.q.productionStatus,
                    'beginCreatetime': vm.q.beginCreatetime,
                    'endCreatetime': vm.q.endCreatetime,
                    'beginPaytime': vm.q.beginPaytime,
                    'endPaytime': vm.q.endPaytime,
                    'payStatus':vm.q.payStatus,
                    'merchantsSalesMan':vm.q.merchantsSalesMan,
                    'onCredit':vm.q.onCredit+'',
                },
                page: 1
            }).trigger("reloadGrid");
        },
        exportExcel() {
            //导出excel
            vm.q.orderId = vm.q.orderId.trim();
            vm.q.name = vm.q.name.trim();
            vm.q.payStatus = vm.q.payStatus+'';
            var data = encodeURIComponent(JSON.stringify(vm.q));
            window.location.href = baseURL + "mercharReport/orderinfo/exportExcel?postData=" + data;
        }
    }
});